const moment = require("moment");
const router = require("koa-router")();
const path = require("path");
const fs = require("fs");
const chalk = require("chalk");

const { pdfDir, pdfDirMonth } = require("../config");
const { buildPdf, buildPdfMonth } = require("../utils");
const genMonthly106Images = require("../utils/genMonthly106Images");
const genWeekly106Images = require("../utils/genWeekly106Images");

/**
 * 导出周诊断报告
 */
router.get("/gen-pdf", async ctx => {
  const startTime = moment();
  let { deviceIdList, pdfNameList, token, companyId, uuid } = ctx.request.query;
  deviceIdList = deviceIdList.split(",");
  pdfNameList = pdfNameList.split(",");
  token = decodeURI(token);
  const folder = path.join(pdfDir, uuid);
  if (!fs.existsSync(folder)) {
    fs.mkdirSync(folder);
  }
  const length = deviceIdList.length;
  try {
    for (let i = 0; i < length; i++) {
      if (deviceIdList[i]) {
        await buildPdf({
          token,
          deviceId: deviceIdList[i],
          name: pdfNameList[i],
          companyId,
          uuid
        }).catch(e => {
          console.log("===========await.catch==============");
          console.error(e);
          console.log("====================================");
          return;
        });
        console.log(`当前生成第${i + 1}个PDF`);
      } else {
        console.log(`deviceId不存在${deviceIdList[i]}`);
      }
    }
    const endTime = moment();
    const diff = endTime.diff(startTime, "minute") + "分钟";
    console.log("=============文件生成时间=============");
    console.info(diff);
    console.log("====================================");
    ctx.body = {
      code: 200,
      message: "操作成功",
      time: diff
    };
  } catch (error) {
    console.log("============for-报错================");
    console.error(error);
    console.log("====================================");
    ctx.body = {
      code: 400,
      error
    };
  }
});

/**
 * 导出月报告
 */
router.get(`/gen-pdf-month`, async ctx => {
  const startTime = moment();
  let { pdfName, token, companyId, uuid } = ctx.request.query;
  token = decodeURI(token);
  const folder = path.join(pdfDirMonth, uuid);
  if (!fs.existsSync(folder)) {
    fs.mkdirSync(folder);
  }
  try {
    await buildPdfMonth({
      token,
      name: pdfName,
      companyId,
      uuid
    }).catch(e => {
      console.log("===========月-await.catch==============");
      console.error(e);
      console.log("====================================");
    });
    const endTime = moment();
    const diff = endTime.diff(startTime, "minute") + "分钟";
    console.log("=============月-文件生成时间=============");
    console.info(diff);
    console.log("====================================");
    ctx.body = {
      code: 200,
      message: "操作成功",
      time: diff
    };
  } catch (error) {
    console.log("============月-for-报错================");
    console.error(error);
    console.log("====================================");
    ctx.body = {
      code: 400,
      error
    };
  }
});

/**
 *
 */
router.get(`/gen-image-monthly`, async ctx => {
  try {
    let { companyId, path } = ctx.request.query;
    path = decodeURI(path);
    const images = await genMonthly106Images(companyId, path);
    console.log("====================================");
    console.log(images);
    console.log("====================================");
    ctx.body = {
      code: 200,
      imagesPath: images
    };
  } catch (error) {
    console.log(chalk.red("===================================="));
    console.log(error);
    console.log(chalk.red("===================================="));
    ctx.body = {
      code: 200,
      error: error
    };
  }
});

router.get(`/gen-image-weekly`, async ctx => {
  let { deviceId, path } = ctx.request.query;
  path = decodeURI(path);
  try {
    const images = await genWeekly106Images(deviceId, path);
    console.log("====================================");
    console.log(images);
    console.log("====================================");
    ctx.body = {
      code: 200,
      imagesPath: images
    };
  } catch (error) {
    console.log(chalk.red("===================================="));
    console.log(error);
    console.log(chalk.red("===================================="));
    ctx.body = {
      code: 400,
      error: error
    };
  }
});

module.exports = router;
